<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img :src="$store.state.userInfo.user_pic" alt="" class="avatar" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"
            ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
          >
          <el-menu-item index="1-2"
            ><i class="el-icon-camera"></i>更换头像</el-menu-item
          >
          <el-menu-item index="1-3"
            ><i class="el-icon-key"></i>重置密码</el-menu-item
          >
        </el-submenu>
        <el-menu-item index="2" @click.native="exit"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <div class="aside-avatar">
          <img :src="$store.state.userInfo.user_pic" alt="" />
          <span>{{ $store.state.userInfo.nickname }}</span>
        </div>
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#23262e"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
        >
          <!-- <el-menu-item index="4">
            <i class="el-icon-setting"></i>
            <span slot="title">首页</span>
          </el-menu-item>
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>文章管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/artcate">文章分类</el-menu-item>
              <el-menu-item index="/artlist">文章列表</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>个人中心</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/userinfo">基本资料</el-menu-item>
              <el-menu-item index="/avatar">更换头像</el-menu-item>
              <el-menu-item index="/resetpwd">重置密码</el-menu-item>
            </el-menu-item-group>
          </el-submenu> -->

          <!-- <div v-for="(item, index) in menu" :key="index">
            <el-menu-item :index="item.indexPath" v-if="!item.children">
              <i class="el-icon-setting"></i>
              <span slot="title">首页</span>
            </el-menu-item>
            <el-submenu :index="item.indexPath" v-else>
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item-group
                :index2="item2.indexPath"
                v-for="(item2, index2) in item.children"
                :key="index2"
              >
                <el-menu-item :index="item2.indexPath">{{
                  item2.title
                }}</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </div> -->
          <el-submenu
            :index="item.indexPath"
            v-for="(item, index) in menu"
            :key="index"
          >
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ item.title }}</span>
            </template>
            <el-menu-item-group
              :index="item2.indexPath"
              v-for="(item2, index2) in item.children"
              :key="index2"
            >
              <el-menu-item :index="item2.indexPath">{{
                item2.title
              }}</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main> <router-view></router-view> </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>心好累</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { getMenu } from '@/api'
export default {
  name: 'Main',
  data () {
    return {
      menu: []
    }
  },
  methods: {
    async exit () {
      // this.$confirm('退出登录, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.$message({
      //       type: 'success',
      //       message: '退出成功!'
      //     })
      //     this.$store.commit('updateToken', '')
      //     localStorage.removeItem('vuex')
      //     this.$router.push('/login')
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消退出'
      //     })
      //   })
      const res = await this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      // console.log(res)
      if (res === 'confirm') {
        this.$message({
          type: 'success',
          message: '退出成功!'
        })
        this.$store.commit('updateToken', '')
        localStorage.removeItem('vuex')
        this.$router.push('/login')
      }
    },
    async getMenuInfo () {
      const res = await getMenu()
      console.log(res)
      this.menu = res.data.data
    }
  },
  created () {
    this.$store.dispatch('userInfoHandle')
    this.getMenuInfo()
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100vh;
  .el-header,
  .el-aside {
    background-color: #23262e;
    .aside-avatar {
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 10px 0;
      img {
        width: 40px;
        height: 40px;
        margin-right: 7px;
        border-radius: 50%;
      }
      span {
        color: #fff;
        font-size: 12px;
      }
    }
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
